package Entity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import Control.CommonControls;
import Control.MySQLController;

public class Shop_Product {
	
	private String productId;
	private String name;
	private String description;
	private String frontImageUrl;
	private String backImageUrl;
	private Date createDate;
	private int numPurchase;
	private double unitPrice;
	private Shop_Catalog catalog;
	private Shop_Product_Catalog_StatusCode productStatus;
	private String workshopId;
	
	public Shop_Product(String productId){
		this.setProductId(productId);
	}

	public Shop_Product(String productId, String name, String description, String frontImageUrl, String backImageUrl, Date createDate, int numPurchase, double unitPrice, String catalogId, String statusCode, String workshopId) {
		this.productId = productId;
		this.name = name;
		this.description = description;
		this.frontImageUrl = frontImageUrl;
		this.backImageUrl = backImageUrl;
		this.createDate = createDate;
		this.numPurchase = numPurchase;
		this.unitPrice = unitPrice;
		this.setCatalog(new Shop_Catalog(catalogId));
		this.productStatus = new Shop_Product_Catalog_StatusCode(statusCode);
		this.workshopId = workshopId;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getFrontImageUrl() {
		return frontImageUrl;
	}

	public void setFrontImageUrl(String frontImageUrl) {
		this.frontImageUrl = frontImageUrl;
	}

	public String getBackImageUrl() {
		return backImageUrl;
	}

	public void setBackImageUrl(String backImageUrl) {
		this.backImageUrl = backImageUrl;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public int getNumPurchase() {
		return numPurchase;
	}
	
	public void setNumPurchase(int numPurchase) {
		this.numPurchase = numPurchase;
	}

	public double getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(double unitPrice) {
		this.unitPrice = unitPrice;
	}

	public Shop_Catalog getCatalog() {
		return catalog;
	}

	public void setCatalog(Shop_Catalog catalog) {
		this.catalog = catalog;
	}

	public Shop_Product_Catalog_StatusCode getProductStatus() {
		return productStatus;
	}

	public void setProductStatus(Shop_Product_Catalog_StatusCode productStatus) {
		this.productStatus = productStatus;
	}
	
	public String getWorkshopId() {
		return workshopId;
	}

	public void setWorkshopId(String workshopId) {
		this.workshopId = workshopId;
	}

	public static ArrayList<Shop_Product> getProducts(){
		ArrayList<Shop_Product> productList = new ArrayList<Shop_Product> ();
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.product";
		ResultSet rs = db.readRequest(sql);
		try {
			while(rs.next()){
				Shop_Product product = new Shop_Product(rs.getString("product_id"), rs.getString("name"), rs.getString("description"), rs.getString("front_image_url"), rs.getString("back_image_url"), rs.getDate("create_date"), rs.getInt("num_purchase"), rs.getDouble("unit_price"), rs.getString("catalog_id"), rs.getString("product_status_code"), rs.getString("workshop_id"));
				productList.add(product);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return productList;
	}
	
	public static ArrayList<Shop_Product> getProductsUnderCat(String catId){
		ArrayList<Shop_Product> productListUnderCat = new ArrayList<Shop_Product> ();
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.product WHERE catalog_id = '" + catId + "'";
		ResultSet rs = db.readRequest(sql);
		try {
			while(rs.next()){
				Shop_Product product = new Shop_Product(rs.getString("product_id"), rs.getString("name"), rs.getString("description"), rs.getString("front_image_url"), rs.getString("back_image_url"), rs.getDate("create_date"), rs.getInt("num_purchase"), rs.getDouble("unit_price"), rs.getString("catalog_id"), rs.getString("product_status_code"), rs.getString("workshop_id"));
				productListUnderCat.add(product);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return productListUnderCat;
	}
	
	public static ArrayList<Shop_Product> getProductsUnderCategory(String catId){
		ArrayList<Shop_Product> productListUnderCat = new ArrayList<Shop_Product> ();
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.product WHERE workshop_id = '" + catId + "'";
		ResultSet rs = db.readRequest(sql);
		try {
			while(rs.next()){
				Shop_Product product = new Shop_Product(rs.getString("product_id"), rs.getString("name"), rs.getString("description"), rs.getString("front_image_url"), rs.getString("back_image_url"), rs.getDate("create_date"), rs.getInt("num_purchase"), rs.getDouble("unit_price"), rs.getString("catalog_id"), rs.getString("product_status_code"), rs.getString("workshop_id"));
				productListUnderCat.add(product);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
		return productListUnderCat;
	}
	
	public static ArrayList<Shop_Product> getSearchProducts(ArrayList<String> keywords){
		ArrayList<Shop_Product> searchProducts = new ArrayList<Shop_Product>();
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.product WHERE";
		for(int i = 0; i < keywords.size(); i++){
			sql += " name LIKE '%" + keywords.get(i) + "%'";
			if(i < keywords.size() - 1)
				sql += " OR";
		}
		ResultSet rs = db.readRequest(sql);
		try {
			while(rs.next()){
				Shop_Product product = new Shop_Product(rs.getString("product_id"), rs.getString("name"), rs.getString("description"), rs.getString("front_image_url"), rs.getString("back_image_url"), rs.getDate("create_date"), rs.getInt("num_purchase"), rs.getDouble("unit_price"), rs.getString("catalog_id"), rs.getString("product_status_code"), rs.getString("workshop_id"));
				searchProducts.add(product);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.terminate();
		
		return searchProducts;
		
	}
	
	public void getProductLinkToCategory(){
		ResultSet rs = null;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT workshop_id FROM ooadpj.product";
		rs = db.readRequest(sql);
		try {
			if(rs.next()){
				workshopId = rs.getString("workshop_id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			db.terminate();
		}
	}
	
	public boolean updateProduct(){
		boolean success = false;
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "UPDATE ooadpj.product SET name = '" + name + "', description = '" + description + "', front_image_url = '" + frontImageUrl + "', back_image_url = '" + backImageUrl + "', unit_price = '" + unitPrice + "', catalog_id = '" + catalog.getId() + "', product_status_code = '" + productStatus.getStatusCode() + "', workshop_id = '" + workshopId + "' WHERE product_id = '" + productId + "'";
		if(db.updateRequest(sql) == 1){
			success = true;
		}
		db.terminate();
		return success;
	}
	
	public boolean createProduct(){
		boolean success = false;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "INSERT INTO ooadpj.product(product_id, name, description, front_image_url, back_image_url, create_date, unit_price, catalog_id, product_status_code, workshop_id) VALUES ('" + productId + "', '" + name + "', '" + description + "', '" + frontImageUrl + "', '" + backImageUrl + "', '" + sdf.format(CommonControls.getCurrentDate()) + "', '" + unitPrice + "', '" + catalog.getId() + "', 'AVA', '" + workshopId + "')";
		if(db.updateRequest(sql) == 1){
			success = true;
		}
		db.terminate();
		return success;
	}

}
